using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Rally_Dekstop
{
 
    public partial class HtmlEditor : UserControl
    {
        private bool isEditMode = false;
        private string htmlText = string.Empty;
        private ContextMenu htmlEditorMenu = new ContextMenu();
        private MenuItem mnuCopy = new MenuItem();
        private MenuItem mnuPaste = new MenuItem();
       
        public HtmlEditor()
        {
            InitializeComponent();
            this.mnuCopy.Index = 0;
            this.mnuCopy.Text = "Copy";
            this.mnuCopy.Click += new EventHandler(mnuCopy_Click);

            this.mnuPaste.Index = 1;
            this.mnuPaste.Text = "Paste";
            this.mnuPaste.Click += new EventHandler(mnuPaste_Click);

            this.htmlEditorMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuCopy, this.mnuPaste});

            this.htmlEditor1.ContextMenu = htmlEditorMenu;
            
        }

        void mnuCopy_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.Copy();
        }

        void mnuPaste_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.Paste();
        }

        public bool isTextEmpty()
        {
            bool istextEmpty = false;
            string textInControl = this.htmlEditor1.GetTextInControl();
            if (!string.IsNullOrEmpty(textInControl))
            {
                textInControl = textInControl.Trim();
                if (textInControl.Length == 0)
                {
                    istextEmpty = true;
                }
            }
            else
                istextEmpty = true;

            return istextEmpty;
           
        }

        
        private string TextInControl
        {
            get { return this.htmlEditor1.GetDocumentSource(); }
            set { this.htmlEditor1.LoadDocument(value); }
        }

        public string HTMLText
        {
            get { return Utilities.HtmlParser("body", this.htmlEditor1.GetDocumentSource(),HtmlParserOption.Outter_Html); }
            set
            {
                try
                {
                    this.htmlEditor1.LoadDocument(value);
                }
                catch
                { }
            }
        }
        
        
        [Category("Appearence")]
        [DefaultValue(false)]
        [Description("This property sets the mode of the HTML Editor")]
        public bool IsEditMode
        {
            get
            {
                return this.isEditMode;
            }
            set
            {
                
                this.isEditMode = value;

                if (isEditMode)
                {
                    toolStrip1.Visible = true;
                    try
                    {
                        this.htmlEditor1.IsDesignMode = true;
                    }
                    catch { }
                   
                }
                else
                {
                    toolStrip1.Visible = false;
                    try
                    {
                        this.htmlEditor1.IsDesignMode = false;
                    }
                    catch { }
                    
                }
            }
        }

        private void boldToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.SetSelectionBold();
        }

        private void italicToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.SetSelectionItalic();
        }

        private void underlineToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.SetSelectionUnderline();
        }

        private void alignLeftToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.SetSelectionALIGNLEFT();
        }

        private void alignCenterToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.SetSelectionALIGNCENTER();
        }

        private void alignRightToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.SetSelectionALIGNRIGHT();
        }

        private void bulletsToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.SetUnOrderedList();
        }

        private void OrderedList_ToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.SetOrderedList();
        }

        private void cutToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.Cut();
        }

        private void copyToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.Copy();
        }

        private void pasteToolStripButton_Click(object sender, EventArgs e)
        {
            this.htmlEditor1.Paste();
        }

        private void htmlEditor1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.B:
                        this.htmlEditor1.SetSelectionBold();
                        break;
                    case Keys.C:
                        this.htmlEditor1.Copy();
                        break;
                    case Keys.U:
                        this.htmlEditor1.SetSelectionUnderline();
                        break;
                    case Keys.I:
                        this.htmlEditor1.SetSelectionItalic();
                        break;
                    case Keys.V:
                        this.htmlEditor1.Paste();
                        break;
                    case Keys.X:
                        this.htmlEditor1.Cut();
                        break;
                    case Keys.Z:
                        this.htmlEditor1.Undo();
                        break;
                    case Keys.Y:
                        this.htmlEditor1.Redo();
                        break;
                }


            }
        }

               
    }
}
